package com.aoxon.cargo.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Banner;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.ImageUtil;

public class BannerManageActivity extends NewBaseActivity {

	/**
	 * 商家自己的banner管理
	 */
	List<Banner> banners = new ArrayList<Banner>();
	LinearLayout llContent, llAdd;
	LayoutInflater inflater;

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.banner_manage;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		llContent = (LinearLayout) findViewById(R.id.ll_banner_content);
		llAdd = (LinearLayout) findViewAndSetOnClick(R.id.ll_banner_add);
		
		inflater = LayoutInflater.from(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		Bitmap bitmap;
		switch(requestCode){
		case MyStateUtil.FOLDER_for_FECADE:

			try {
				String path = ImageUtil.getAbsoluteImagePath(data.getData(), this);
				bitmap = ImageUtil.getImageBitmap(path, 800, 200);
				View view = inflater.inflate(R.layout.banner_manage_item, null);
				MyBannerImageView iv = (MyBannerImageView)view.findViewById(R.id.iv_banner);
				iv.setImageBitmap(bitmap);
				llContent.addView(view, banners.size());
				banners.add(new Banner());
				
				
				
//				if (bitmap.getWidth() < 200 || bitmap.getHeight() < 200) {
//					ToastUtil.show(this,
//							"图片分辨率不要小于200x200");
//					Log.v(tag, " 图片太小 不要小于200x200 ");
//				} else {
//					mySquarePicture.getImageView(path, false);
//					picture.setPictureUrl(path);
//					picture.setPictureType(MyReturnState.STALL_TYPE);
//					picture.setPictureState(DataState.INITIAL);
//					pictureList.add(picture);
//				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
			
		}
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.ll_banner_add:
			addBanner();
			break;
		}
	}

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub

	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub

	}

	public void addBanner() {
		// TODO Auto-generated method stub
		Intent intent = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(intent, MyStateUtil.FOLDER_for_FECADE);
	}

}

class MyBannerImageView extends ImageView {

    public MyBannerImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MyBannerImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyBannerImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width, width/2);
    }
    
}
